package com.isoftstone;

import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;

import java.io.*;

public class Main {
    public static void main(String[] args) {
        String endPoint = "obs.cn-south-1.myhuaweicloud.com";
        String ak="VQ9M9TWWMP3OPQIMYLOX";
        String sk="F8rT3BsDoNHExuVZClcVCSZqEhBBtYKycWZgIOsf";
        String backetName="ysm-group2";

        ObsClient obsClient = new ObsClient(ak,sk,endPoint);
        try( FileInputStream fis = new FileInputStream("E:\\wsssshuan\\ju.jpg");
        ){
            obsClient.putObject(backetName,"bird",fis);
        }catch (IOException e){
            e.printStackTrace();
        }

        try(FileOutputStream fileOutputStream = new FileOutputStream("E:\\wsssshuan\\ju.jpg1.jpg");){
            ObsObject obsObject = obsClient.getObject(backetName, "bird");
            InputStream inputStream = obsObject.getObjectContent();
            byte[] block = new byte[1024];
            int lengh = 0;
            while((lengh = inputStream.read(block))> 0){
                fileOutputStream.write(block,0,lengh);
                fileOutputStream.flush();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

      //  obsClient.deleteObject(backetName, "bird");





    }
}

//    @Override
//    public PadingRstType<FileObsEntity> getClassInfolist() {
//        ObsClient obsClient =  getSigleInstance();
//        ObjectListing objectListing=obsClient.listObjects(backetName);
//        List<ObsObject> obsObjects = objectListing.getObjects();
//
//        PadingRstType<FileObsEntity> prt = new PadingRstType<FileObsEntity>();
//        FileObsEntity fileObsEntity;
//        List<FileObsEntity> list = new ArrayList<>();
//
//        for(ObsObject obsObject:obsObjects){
//            fileObsEntity= new FileObsEntity();
//            fileObsEntity.setLastModified(DataUtil.formatDate(obsObject.getMetadata().getLastModified()));
//            fileObsEntity.setContentLength(FileUtil.clcltn(obsObject.getMetadata().getContentLength()/1024f));
//            fileObsEntity.setObjectKey(obsObject.getObjectKey());
//            list.add(fileObsEntity);
//        }
//        prt.setTotal(obsObjects.size());
//        prt.setRawRecords(list);
//        prt.putItems();
//        return prt;
//    }

//    public static void saveFile(InputStream fis, File descFile) throws IOException
//    {
//
//        FileOutputStream fos = null;
//        byte[] buffer = new byte[1024];
//        int readLen = 0;
//        fos = new FileOutputStream(descFile);
//        while ((readLen = fis.read(buffer)) != -1)
//        {
//            fos.write(buffer, 0, readLen);
//        }
//        fos.flush();
//        fis.close();
//        fos.close();
//
//        System.out.println("下载成功！");
//    }